﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Windows.Input;
using IntoNails.Infrastructure;
using IntoNails.Infrastructure.InteractionRequests;
using IntoNails.Models;
using IntoNails.Modules.TicketManagement.EventAggregators;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;

namespace IntoNails.Modules.TicketManagement.ViewModels
{
    [Export(typeof(ITicketRibbonTabViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class TicketRibbonTabViewModel : NotificationObject, ITicketRibbonTabViewModel
    {
        #region Fields

        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager _regionManager;

        private bool _isTabSelected;

        #endregion

        [ImportingConstructor]
        public TicketRibbonTabViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
        {
            this._eventAggregator = eventAggregator;
            this._regionManager = regionManager;

            this.CreateNewTicketCommand = new DelegateCommand(this.OnCreateNewTicketCommandExecuted);
            this.EditTicketCommand = new DelegateCommand(this.OnEditTicketCommandExecuted);
            this.DeleteTicketCommand = new DelegateCommand(this.OnDeleteTicketCommandExecuted);
        }

        #region Implementation of IHeaderInfoProvider<string>

        public string HeaderInfo
        {
            get { return "Tickets"; }
        }

        #endregion

        #region Properties

        public bool IsTabSelected
        {
            get { return this._isTabSelected; }
            set
            {
                this._isTabSelected = value;

                if (value)
                {
                    Uri workspaceUri = new Uri("TicketManagementWorkspaceView", UriKind.Relative);
                    this._regionManager.RequestNavigate(RegionNames.WorkspaceRegion, workspaceUri);
                }

                base.RaisePropertyChanged(() => IsTabSelected);
            }
        }

        #endregion

        #region Implementation of ICustomerRibbonTabViewModel

        public ICommand CreateNewTicketCommand { get; private set; }
        public ICommand EditTicketCommand { get; private set; }
        public ICommand DeleteTicketCommand { get; private set; }

        #endregion

        #region Command Action Handlers

        private void OnCreateNewTicketCommandExecuted()
        {
            Ticket publishedCustomer = Ticket.CreateNewTicket();

            this._eventAggregator.GetEvent<CreateNewTicketEventAggregator>().Publish(publishedCustomer);
        }

        private void OnEditTicketCommandExecuted()
        {
            this._eventAggregator.GetEvent<EditTicketEventAggregator>().Publish(null);
        }

        private void OnDeleteTicketCommandExecuted()
        {
            MessageBoxContext context = new MessageBoxContext("Delete this Ticket?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcons.Question);

            this._eventAggregator.GetEvent<DeleteTicketEventAggregator>().Publish(context);
        }

        #endregion
    }
}
